package com.example.elysiumease;

import com.example.elysiumease.mapper.SinMapper;
import com.example.elysiumease.model.Sin;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional
@Commit
public class SinMapperTest {

    @Autowired
    private SinMapper sinMapper;

    @Test
    void testInsertSin() {
        Sin sin = new Sin();
        sin.setSinName("TEST_SIN");
        sin.setSeverityLevel((short) 5);
        sin.setScoreImpact(new BigDecimal("0.50"));

        int result = sinMapper.insertSin(sin);
        assertEquals(1, result);
        assertNotNull(sin.getSinId());
    }

    @Test
    void testUpdateSin() {
        // 插入一条记录
        Sin sin = new Sin();
        sin.setSinName("TEST_SIN");
        sin.setSeverityLevel((short) 5);
        sin.setScoreImpact(new BigDecimal("0.50"));
        sinMapper.insertSin(sin);

        // 更新记录
        sin.setSinName("UPDATED_TEST_SIN");
        sin.setSeverityLevel((short) 7);
        sin.setScoreImpact(new BigDecimal("0.70"));
        int result = sinMapper.updateSin(sin);
        assertEquals(1, result);

        // 验证更新结果
        Sin updatedSin = sinMapper.selectById(sin.getSinId());
        assertEquals("UPDATED_TEST_SIN", updatedSin.getSinName());
        assertEquals((short) 7, updatedSin.getSeverityLevel());
        assertEquals(new BigDecimal("0.70"), updatedSin.getScoreImpact());
    }

    @Test
    void testDeleteSin() {


        // 删除记录
        int result = sinMapper.deleteSin((short) 1);
        assertEquals(1, result);

        // 验证删除结果
        Sin deletedSin = sinMapper.selectById((short)1);
        assertNull(deletedSin);
    }

    @Test
    void testSelectById() {
        // 查询记录
        Sin dbSin = sinMapper.selectById((short) 2);
        assertNotNull(dbSin);
        assertNotNull(dbSin.getSinId());
    }

    @Test
    void testSelectAll() {
        // 插入两条记录
        Sin sin1 = new Sin();
        sin1.setSinName("TEST_SIN_1");
        sin1.setSeverityLevel((short) 5);
        sin1.setScoreImpact(new BigDecimal("0.50"));
        sinMapper.insertSin(sin1);

        Sin sin2 = new Sin();
        sin2.setSinName("TEST_SIN_2");
        sin2.setSeverityLevel((short) 7);
        sin2.setScoreImpact(new BigDecimal("0.70"));
        sinMapper.insertSin(sin2);

        // 查询所有记录
        List<Sin> sins = sinMapper.selectAll();
        assertNotNull(sins);
        assertFalse(sins.isEmpty());
    }
}